import { defineStore } from 'pinia';
import Cookies from 'js-cookie';
import { Session } from '/@/utils/storage';
import { useLoginApi } from '/@/api/login';

const loginApi = useLoginApi();

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
	state: (): UserInfoState => ({
		userInfos: {
			id: '',
			name: '',
			phone: '',
			password: '',
			photo: '',
			email: '',
			roleCodeList: [],
		},
	}),
	actions: {
		async setUserInfos() {
			// 存储用户信息到浏览器缓存
			if (Session.get('userInfo')) {
				this.userInfos = Session.get('userInfo');
			} else {
				const token = Session.get('token');
				const param = {
					'token': token,
				};
				const UserInfoData = await loginApi.getUserInfoByToken(param);
				const userInfo: UserInfo = UserInfoData.data;
				Session.set('userInfo', userInfo);
				this.userInfos = userInfo;
			}
		},
	},
});
